<?php

/**
 * 应用函数库
 */

function viewLinks($num = 10)
{
    $LinksModel = new Model('links');
    $links = $LinksModel->fetchAll();
    foreach ($links as $link) {
        echo '<li style="padding: 0em 0.5em;display: inline;"><a href="' . $link['url'] .
            '">' . $link['title'] . '</a></li>';
    }
}

function viewSections($forumID)
{
    $SectionModel = new Model('section');
    $sections = $SectionModel->fetchAll('*', array('fid' => $forumID));
    foreach ($sections as $section) {
        echo '<li><a href="' . Controller::url('forum', 'section', array('id' => $section['id'])) .
            '">' . $section['name'] .
            '</a><span style="vertical-align: top;vertical-position: subscript;font-size: 12px;margin-left: 0.5em;">(<span style="color: red;vertical-align: super;vertical-position: superscript;">' .
            $section['today'] . '</span>)</span></li>' . "\r\n";
    }
}

function viewAnnouncement($num)
{
    $AnnouncementModel = new Model('announcement');
    $announcements = $AnnouncementModel->select('*')->order('id', false)->limit($num)->
        fetchAll();
    foreach ($announcements as $announcement) {
        echo '<li><a href="' . Controller::url('view', 'announcement', array('id' => $announcement['id'])) .
            '">' . $announcement['title'] . '</a></li>' . "\r\n";
    }
}

function viewTopic($sid, $start, $num)
{
    $TopicModel = new Model('topic');
    $topics = $TopicModel->select('*')->where("sid='{$sid}'")->order('ptime', false)->
        limit($start, $num)->fetchAll();
    if (empty($topics)) {
        echo '<div id="tip">暂无话题</div>';
    } else {
        $UserModel = new Model('user');
        foreach ($topics as $topic) {
            $user = $UserModel->fetch('*', array('id' => $topic['uid']));
            if (checkVip($user['vip'])) {
                $style = 'vip';
            } else {
                $style = 'common';
            }
            echo '<li id="' . $style . '">';
            echo '<p><a href="' . Controller::url('view', 'topic', array('id' => $topic['id'])) .
                '">' . $topic['title'] . '</a></p>';
            echo '<span><img src="/Application/static/pic/vip.png" alt="VIP" title="VIP" /><a href="' .
                Controller::url('user', 'home', array('id' => $user['id'])) . '">' . $user['user'] .
                '</a></span>';
            echo '</li>', "\r\n";
        }
    }
}

function checkVip($time)
{
    $time = empty($time) ? '0' : $time;
    if ($time < time()) {
        //已经到期
        return false;
    } else {
        //没有到期
        return true;
    }
}

function checkVipByID($uid)
{
    $UserModel = new Model('user');
    $user = $UserModel->fetch('*', array('id' => $uid));
    return checkVip($user['vip']);
}

/**
 * 格式化输出提示时间
 */
function dateFormat($timestamp)
{
    $time = (time() - $timestamp) + 0;
    if ($time < 60) {
        return $time . '秒前';
    } elseif ($time < 3600) {
        return date('i分钟前', $time);
    } elseif ($time < 86400) {
        return date('H小时前', $time);
    } elseif ($time < 604800) {
        //如果小于七天
        return date('d天前', $time);
    } else {
        //大于七天直接返回时间
        return date('Y年m月d日', $timestamp);
    }
}

?>